package de.ars.client.items;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import de.ars.client.animator.AbstractSlideItemAnimator;
import de.ars.client.animator.Animator;
import de.ars.client.animator.IAnimationEventListener;
import de.ars.client.animator.Point;
import de.ars.client.items.animators.AddPersonItemAnimator;
import de.ars.client.items.elements.AbstractItem;
import de.ars.client.paintSystems.ClassicLayout;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class AbstractSlideItem implements IAnimationEventListener 
{
	protected static final int COLOR_BORDER = 0x0010475f;
	protected static final int COLOR_WINDOW_BACKGROUND = 0x0025afd4;
    
    public static final int ANIMATION_OPEN = 0;
    public static final int ANIMATION_CLOSE = 1;
    public static final int ANIMATION_SCROLL_UP = 2;
    public static final int ANIMATION_SCROLL_DOWN = 3;
    
    protected static final int STATE_OPEN_ANIMATION = 0;
    protected static final int STATE_OPENED = 1;
    protected static final int STATE_CLOSE_ANIMATION = 2;
    protected static final int STATE_CLOSED = 3;
    protected int state;
    
    protected int animationType;
    protected int selectedItemIndex;

    protected int screenWidth;
    protected int screenHeight;
	
	protected AbstractSlideItemAnimator animator = null;
	protected StringPainter stringPainter;
	protected ClassicLayout layout;
	protected MainScreen parent;
	
	protected Vector entryItems;
	
	public AbstractSlideItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		this.screenWidth = _width;
		this.screenHeight = _height;
		this.state = STATE_CLOSED;
		this.animationType = -1;
		this.selectedItemIndex = 0;
		
		this.parent = _parent;
		this.layout = new ClassicLayout( this.screenWidth, this.screenHeight );
    	this.stringPainter = _stringPainter;
	}
	
	public boolean isOpen()
    {
    	return this.state != STATE_CLOSED;
    }
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
			switch(_key)
	    	{
				case -1:
					if(this.selectedItemIndex > 0)
					{
						this.selectedItemIndex--;
					}
					return true;
				case -2:
					if(this.selectedItemIndex < this.entryItems.size()-1)
					{
						this.selectedItemIndex++;
					}
					return true;
	    	}
		return false;
	}
	
	public void open()
	{
		this.animationType = ANIMATION_OPEN;
		this.animator.openItem();
		this.state = STATE_OPEN_ANIMATION;
	}
	
	public void close()
	{
		this.animationType = ANIMATION_CLOSE;
		this.animator.closeItem();
		this.state = STATE_CLOSE_ANIMATION;
	}
	
	public void paint( Graphics g )
	{
		if(this.state != STATE_CLOSED)
		{
			Point pos = this.animator.getPoint( AddPersonItemAnimator.POINT_POS );
	        Point size = this.animator.getPoint( AddPersonItemAnimator.POINT_SIZE );
	        Point textPos = this.animator.getPoint(AddPersonItemAnimator.POINT_TEXT_POS);
	        
	        g.setColor( COLOR_WINDOW_BACKGROUND );
	        g.fillRect( (int)pos.getX(), (int)pos.getY(), (int)size.getX(), (int)size.getY() );
	        g.setColor( COLOR_BORDER );
	        g.drawRect( (int)pos.getX(), (int)pos.getY(), (int)size.getX(), (int)size.getY() );
	        
	        if (this.state != STATE_CLOSED)
	        	this.drawEntries(g, textPos);
		}
	}
	
	public void drawEntries(Graphics _g, Point _pos)
	{
		if (this.entryItems != null)
		{
			AbstractItem currentItem = null;
			int x = (int)_pos.getX();
			int y = (int)_pos.getY();
			int i = 0;
			Enumeration e = this.entryItems.elements();
	        while( e.hasMoreElements() )
	        {
	         	_g.translate( x - _g.getTranslateX(), y - _g.getTranslateY() );
		        currentItem = (AbstractItem)e.nextElement();
		        currentItem.paint( _g, i == this.selectedItemIndex);
		        y += currentItem.getHeight();
		        i++;
	        }
		}
	}
	
	public void animationEvent(int event) 
	{
		if( event == IAnimationEventListener.ANIMATION_STOPPED )
        {
            switch( this.animationType )
            {
            	case ANIMATION_OPEN:
            		this.state = STATE_OPENED;
            		this.animator.resetPosition( true );
            		break;
            	case ANIMATION_CLOSE:
            		this.state = STATE_CLOSED;
            	//	this.selectedItemIndex = 0;
            		this.animator.resetPosition( true );
            		break;
            	case ANIMATION_SCROLL_DOWN:
            		this.animator.resetPosition(true);
            		//this.selectedItemIndex++;
            	case ANIMATION_SCROLL_UP:
            		this.animator.resetPosition(true);
            		//this.selectedItemIndex--;
            }            
            this.animationType = -1;
        }
	}
	
	public boolean isAnimationInProgress()
    {
        return this.animator.getAnimationState() == Animator.ANIMATION_IN_PROGRESS;
    }
	
	public void process( long elapsed )
    {
        this.animator.process( elapsed );
    }
}
